<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/context.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>填报计划</title>
<base target="_self"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="<%=path%>/common/script/context.js" type="text/javascript"></script>
<script src="<%=path%>/common/script/context_from.js" type="text/javascript"></script>
 <!-- 上传组件引入js -->
<link href="<%= path %>/common/swfupload/process.css" rel="stylesheet" type="text/css"/>
<script src="<%= path %>/common/swfupload/swfupload.js" type="text/javascript"></script>
<script src="<%= path %>/common/swfupload/swfupload.queue.js" type="text/javascript"></script>
<script src="<%= path %>/common/swfupload/fileprogress.js" type="text/javascript"></script>
<script src="<%= path %>/common/swfupload/handlers.js" type="text/javascript"></script>
<script src="<%= path %>/common/swfupload/file.js" type="text/javascript"></script>
<script src="<%= path %>/common/script/required/requirePlan.js" type="text/javascript" ></script>
<script language="javaScript">
//附件需要添加的信息
var sessionId="<%=session.getId()%>";
var attachmentType="RequiredMaterial"; //当前是哪个类别功能上传的附件
var path="<%= path %>" 
var uuIdData=[];//已上传的文件的文件uuid，上传后的文件以uuId命名
var fileNameData=[];//已上传的文件名
var fileTypeData=[];//已上传的文件的格式
var attIdData=[];//已存入附件表的附件信息

var num = 1;
function addRow(materialId,buyCode,buyName,materialType,unit,newPrice,amount,mnemonicCode){
	if(!materialId)var materialId="";
	if(!buyCode)var buyCode="";
	if(!buyName)var buyName="";
	if(!materialType)var materialType="";
	if(!unit)var unit="";
	if(!mnemonicCode)var mnemonicCode="";
	if(!newPrice)var newPrice="";
	if(newPrice == "0" || newPrice==""){newPrice = "0"};
	if(!amount)var amount="";
	
	tr=document.getElementById("listtable").insertRow();
    tr.className="input_ys1";
    var cell1 = tr.insertCell(); 
    var cell2 = tr.insertCell();
	var cell3 = tr.insertCell();
	var cell4 = tr.insertCell();
	var cell5 = tr.insertCell();
	var cell6 = tr.insertCell();
	var cell7 = tr.insertCell();
	var cell8 = tr.insertCell();
	var cell9 = tr.insertCell();
	var cell10 = tr.insertCell();
	var cell11 = tr.insertCell();
	var cell12 = tr.insertCell();
	cell1.innerHTML="<input type='hidden' name='rowIndex' value='"+num+"'/><input type='hidden' name='rmdList["+num+"].materialId' id='materialId_"+num+"' value='"+materialId+"'>"
		    +"<button class='btn btn-mini btn-danger' type='button' onclick='deleteRow(this,"+num+")'><i class=\"icon-white icon-trash\"></i></button>";
	cell2.innerHTML="<input type='text' name='rmdList["+num+"].buyCode' id='buyCode_"+num+"' readonly value='"+buyCode+"' size='10' style='width: 95px' onclick='selectMaterial("+num+")'>" ;
	cell3.innerHTML="<span id='buyName_"+num+"'>"+buyName+"</span> <input type='hidden' id='buyName_s_"+num+"' value='"+buyName+"' name='rmdList["+num+"].buyName' />" ;
	cell4.innerHTML="<span id='materialType_"+num+"'>"+materialType+"</span><input type='hidden' id='materialType_s_"+num+"' value='"+materialType+"' name='rmdList["+num+"].materialType' />";
    cell5.innerHTML="<span id='unit_"+num+"'>"+unit+"</span><input type='hidden' id='unit_s_"+num+"' value='"+unit+"' name='rmdList["+num+"].unit' />";
    cell6.innerHTML="<input type='text' name='rmdList["+num+"].amount'  id='amount_"+num+"' value='"+amount+"' style='width: 35px' onkeyup='validateNum(this);caculatePriceByAmt(this,"+num+")'>";
	if(newPrice!="0"){
		cell7.innerHTML="<div id='newPrice_"+num+"'>"+newPrice+"<img src='<%= path %>/images/chart_curve.png' style='cursor:pointer' onclick='viewHistoryPrice("+materialId+")'/></div> <input type='hidden' id='newPrice_s_"+num+"' value='"+newPrice+"' name='rmdList["+num+"].newPrice' />" ;
	 }else{
		cell7.innerHTML="<div id='newPrice_"+num+"'></div> <input type='hidden' id='newPrice_s_"+num+"' value='' name='rmdList["+num+"].newPrice' />" ;
	}
	cell8.innerHTML="<input type='text' name='rmdList["+num+"].estimatePrice' value='' id='estimatePrice_"+num+"' style='width: 50px' onkeyup='caculatePrice(this,"+num+");'>" ;
	cell9.innerHTML="<span id='estimateSumPrice_"+num+"'></span> <input type='hidden' id='estimateSumPrice_s_"+num+"' value='0' name='rmdList["+num+"].estimateSumPrice' />" ;
    cell10.innerHTML="<input type='text' name='rmdList["+num+"].deliverDate' id='deliverDate_"+num+"'  class=\"Wdate\" onchange =\"deliverDateFor('"+num+"')\" style='width:90px' "
		    					+"onclick=\"WdatePicker({ dateFmt:'yyyy-MM-dd' })\" readonly>";
	cell11.innerHTML="<input type='hidden' name='rmdList["+num+"].mnemonicCode' id='mnemonicCode_"+num+"' value='"+mnemonicCode+"'><input type='text' name='rmdList["+num+"].deliverPlace' id='deliverPlace_"+num+"'  size='15' style='width: 90px'>" ;
	cell12.innerHTML="<input name='rmdList["+num+"].remark' type='text' style='width: 90%'/>" ;
	num++;
}
	//删除一行
	function deleteRow(obj,num){
	    var priceSumStr=$("#estimateSumPrice_s_"+num).val();
	    if(priceSumStr!=""){
	        var totalPriceStr=$("#totalBudget").val();
	        totalPrice=FloatSub(totalPriceStr,priceSumStr);
		    document.getElementById("totalBudget_s").innerText =parseFloat(totalPrice).toFixed(2);
		    $("#totalBudget").val(parseFloat(totalPrice).toFixed(2));
	    } 
		$(obj).parent().parent().remove(); 
	}
	
	var acd;
	//选择采购对象
	function selectMaterial(rowId){
        acd=rowId;
		var url="viewMaterialInfoIndex_requiredPlan.action";		
		createdetailwindow("选择服务编码",url,3);
	}

   //选择采购对象
   function valueMaterial(){
	   var rowId=acd;	
	   var winObj=document.getElementById('returnVals').value;
		if(winObj){
			var returnVals = winObj.split('@');
			var rmSource = returnVals[0];
			for(var i=1;i<returnVals.length;i++){
				var materialId = returnVals[i].split("#")[0];
				var buyCode = returnVals[i].split("#")[1];
				var buyName = returnVals[i].split("#")[2];
				var materialType = returnVals[i].split("#")[3];
				var unit = returnVals[i].split("#")[4];
				var newPrice = returnVals[i].split("#")[5];
				if(newPrice == "0" || newPrice==""){newPrice = "0"};
				var mnemonicCode = returnVals[i].split("#")[6];
				var amount = returnVals[i].split("#")[7];
				
				if(amount=="" || typeof(amount)=="undefined") amount=0;
				
				if(i==1){
				    $('#materialId_'+rowId).val(materialId);
					$('#buyCode_'+rowId).val(buyCode);
					$('#buyName_'+rowId).html(buyName);
					$('#buyName_s_'+rowId).val(buyName);
					$('#materialType_'+rowId).html(materialType);
					$('#materialType_s_'+rowId).val(materialType);					
					$('#unit_'+rowId).html(unit);
					$('#unit_s_'+rowId).val(unit);
					if(newPrice!="0"){
					  $('#newPrice_'+rowId).html(newPrice+"<img src='<%= path %>/images/chart_curve.png' style='cursor:pointer' onclick='viewHistoryPrice("+materialId+")'/>");
					  $('#newPrice_s_'+rowId).val(newPrice);
					}else{
					  $('#newPrice_'+rowId).html("");
					  $('#newPrice_s_'+rowId).val("");
					}		
					$('#mnemonicCode_'+rowId).html(mnemonicCode);
					$('#amount_'+rowId).val(amount);
					$('#estimatePrice_'+rowId).val("");
				}else{
					addRow(materialId,buyCode,buyName,materialType,unit,newPrice,amount,mnemonicCode);
				}
			}
		}	
     }
     function selectBuyType(obj){
         var buyType=obj.value;
         var TpurchaseDeptId=$('#TpurchaseDeptId').val();
         var TownPurchaseDeptId=$('#TownPurchaseDeptId').val();
         if(buyType=="0"){
             $('#ownPurchaseDept').css("display","block");
             $('#purchaseDept').css("display","none");
             $('#purchaseDeptId').val(TownPurchaseDeptId);
         }else{
             $('#ownPurchaseDept').css("display","none");
             $('#purchaseDept').css("display","block");
             $('#purchaseDeptId').val(TpurchaseDeptId);
         }
     }
</script>
<style type="text/css">
	.input_ys1{text-align:center;}
</style>
</head>
<body>

<div class="container-fluid">
<form class="defaultForm" id="requiredPlan_save" method="post" action="saveRequiredPlan_requiredPlan.action" enctype="">
<input type="hidden" name="requiredMaterial.rmCode" value=""/>
<input type="hidden" name="requiredMaterial.floatCode" value=""/>

<input type="hidden" name="requiredMaterial.attIds" id="attIds" />
<input type="hidden" name="requiredMaterial.fileNameData" id="fileNameData" value=""/>
<input type="hidden" name="requiredMaterial.uuIdData" id="uuIdData" value=""/>
<input type="hidden" name="requiredMaterial.fileTypeData" id="fileTypeData" value=""/>
<input type="hidden" name="requiredMaterial.attIdData" id="attIdData" value=""/>
<input type="hidden" name="returnVals" id="returnVals" value=""/>
<!-- 防止表单重复提交 -->
<s:token/>
	<div  class="row-fluid">
			<table class="table_ys1" width="100%">
    				<tr>
		          		<td  colspan="4" class="Content_tab_style_04">填报计划</td>
		        	</tr>
		        	<tr>
						<td width="15%" class="Content_tab_style1">计划名称：</td>
						<td width="35%" class="Content_tab_style2">
							<input class="Content_input_style1" datatype="*" nullmsg="计划名称不能为空！" type="text" id='rmName' name="requiredMaterial.rmName" /><font color="#FF0000">*</font>
							<div class="info"><span class="Validform_checktip">计划名称不能为空！</span><span class="dec"><s class="dec1">&#9670;</s><s class="dec2">&#9670;</s></span></div>
						</td>
						<td width="15%" class="Content_tab_style1">计划类型：</td>
						<td width="35%" class="Content_tab_style2">
						    <select name="requiredMaterial.rmType" id="rmType">
							<c:forEach items="${requireType}" var="requireType">
							      <option value="${requireType.key }">${requireType.value }</option>
							</c:forEach>
							</select>
						</td>
					</tr>
					<tr>
						<td width="15%" class="Content_tab_style1">采购类型：</td>
						<td width="35%" class="Content_tab_style2">
						    <select name="requiredMaterial.buyType" id="buyType" onchange="selectBuyType(this);">
							<c:forEach items="${buyType}" var="buyType">
							      <option value="${buyType.key }">${buyType.value }</option>
							</c:forEach>
							</select>
						</td>						
						<td class="Content_tab_style1" nowrap>采购组织：</td><td class="Content_tab_style2">
							<span id="ownPurchaseDept">${ownPurchaseDeptName }</span>
							<span id="purchaseDept" style="display: none">${purchaseDeptName }</span>
							<input type="hidden" name="requiredMaterial.purchaseDeptId" id="purchaseDeptId" value="${ownPurchaseDeptId }"/>
							<input type="hidden" id="TpurchaseDeptId" value="${purchaseDeptId }"/>
							<input type="hidden" id="TownPurchaseDeptId" value="${ownPurchaseDeptId }"/>
						</td>						
					</tr>
		    		<tr>
		    		    <td width="15%"  class="Content_tab_style1">预算金额：</td>
						<td width="35%"  class="Content_tab_style2">
							<span id="totalBudget_s"></span>
							<input class="Content_input_style1"  type="hidden" id='totalBudget'   name="requiredMaterial.totalBudget" value="0"/>
						</td>
						<td width="15%"  class="Content_tab_style1" nowrap>填报日期：</td>
						<td width="35%" class="Content_tab_style2">
							<fmt:formatDate value="${requiredMaterial.writeDete}" pattern="yyyy-MM-dd"/>&nbsp;
						</td>						
					</tr>
		    		<tr>
						<td class="Content_tab_style1">填报人：</td>
						<td class="Content_tab_style2">
							${writerCn }
			 				<input type="hidden" id="writer" name="requiredMaterial.writer" value="${writer }"/>
						</td>
						<td class="Content_tab_style1">填报单位：</td>
						<td class="Content_tab_style2">
							${deptName }
							<input type="hidden" name="requiredMaterial.deptId" value="${deptId }"/>
						</td>
					</tr>
					<tr>
						<td  class="Content_tab_style1"><span id="spanButtonPlaceHolder" ></span></td>
						<td class="Content_tab_style2" colspan="3">
							<!-- 附件存放 -->
							<div  id="fileDiv" class="panel"> 
							</div>
							<input class="cancel" id="btnCancel" name="cancelImg" type="button" style="display: none" value="取消" onclick="swfu.cancelQueue();"  />
							<div id="fsUploadProgress" style="padding-left:200px;width: 200px;"></div>
							
						</td>
					</tr>
			</table>
			</div>  
       	<div  class="row-fluid">
       	<table class="table_ys1" id="listtable">
       	<tr>
       	<td colspan="100%"  class="Content_tab_style_05">计划明细</td>
       	</tr>
		<tr id="tdNum" align="center" class="Content_tab_style_04">
			<th width="2%" nowrap > 操作</th>
			<th width="95px" nowrap>编码<font color="#ff0000">*</font></th>
			<th width="10%" nowrap>名称 </th>
			<th width="10%" nowrap>规格型号 </th>
			<th width="10%" nowrap>单位</th>
			<th width="55px" nowrap>数量<font color="#ff0000">*</font></th>
			<th width="80px" nowrap>上次采购价</th>
			<th width="80px" nowrap>估算单价</th>
			<th width="100px" nowrap>估算总价</th>
			<th width="100px" nowrap>交货时间</th>
			<th width="100px" nowrap>交货地点</th>
			<th width="20%" nowrap>备注</th>
		</tr>
		<tr align="center" class="input_ys1">
			<td>
			    <input type="hidden" name="rowIndex" value="0"/><input type='hidden' name='rmdList[0].materialId' id='materialId_0'  value=""/>
			</td>
			<td>
			   <input type='text' id='buyCode_0' name='rmdList[0].buyCode' readonly size='10' style="width: 95px" onclick='selectMaterial(0)'/>
			</td>
			<td>
			   <span id='buyName_0'></span><input type='hidden' id='buyName_s_0' name='rmdList[0].buyName' /> </td>
			<td>
			   <span id='materialType_0'></span> <input type='hidden' id='materialType_s_0' name='rmdList[0].materialType' />
	        </td>
	        <td>
	           <span id='unit_0'></span> <input type='hidden' id='unit_s_0' name='rmdList[0].unit' />
	        </td>
	        <td>
				<input type='text' name='rmdList[0].amount'  id='amount_0'  style="width: 35px" onkeyup="validateNum(this);caculatePriceByAmt(this,0)" />
			</td>
			<td>
			    <div id='newPrice_0'></div><input type='hidden' id='newPrice_s_0' name='rmdList[0].newPrice' />
			</td>			
			<td><input type='text' name='rmdList[0].estimatePrice'  id='estimatePrice_0'  style="width: 50px"  onkeyup="caculatePrice(this,0);" /></td>
			<td><span id='estimateSumPrice_0'></span><input type='hidden' id='estimateSumPrice_s_0' name='rmdList[0].estimateSumPrice' value='0'/></td>
			<td>
				<input type='text' name='rmdList[0].deliverDate' id='deliverDate_0' class='Wdate'   style="width: 90px" onchange ="deliverDateFor('0')"   onclick="WdatePicker({ dateFmt:'yyyy-MM-dd' })" readonly/>
			</td>			
			<td>
				<input type='hidden' name='rmdList[0].mnemonicCode' id='mnemonicCode_0'  value=""/><input type='text' name='rmdList[0].deliverPlace' id='deliverPlace_0'  size='15'  style="width: 90px"/>
			</td>
			<td><input name='rmdList[0].remark' type="text" style="width: 90%"/></td>
		</tr>
	</table>
    </div>
	<div class="buttonDiv">
		<button type="button" class="btn btn-info" type="button" id="btn-add" onclick="javascript:addRow();"><i class="icon-white icon-plus-sign"></i> 添加一行</button>
		<button class="btn btn-success" id="btn-save" ><i class="icon-white icon-ok-sign"></i>保存</button>
		<button class="btn btn-cacel" type="button" id="btn-cacel"  onclick="javascript:history.go(-1);" ><i class="icon icon-repeat"></i>返回</button>
	 </div>	
</form>
 </div>
 <script type="text/javascript">
$(function(){
		$(".defaultForm").Validform( {
			btnSubmit : "#btn-save",
			tiptype : function(msg, o, cssctl) {
				if (!o.obj.is("form")) {//验证表单元素时o.obj为该表单元素，全部验证通过提交表单时o.obj为该表单对象;
				var objtip = o.obj.parents("td").find(".Validform_checktip");
				cssctl(objtip, o.type);
				objtip.text(msg);

				var infoObj = o.obj.parents("td").find(".info");
				if (o.type == 2) {
					infoObj.fadeOut(200);
				} else {
					if (infoObj.is(":visible")) {
						return;
					}
					var left = o.obj.offset().left, top = o.obj.offset().top;

					infoObj.css( {
						left : left + 10,
						top : top - 45
					}).show().animate( {
						top : top - 35
					}, 200);
				}

			}
		},
		beforeSubmit : function(curform) {
			//在验证成功后，表单提交前执行的函数，curform参数是当前表单对象。
			//这里明确return false的话表单将不会提交;

			save();
			return false;
		}
		});
	});
	
</script>
</body>
</html>